import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class IntercalateIndex extends StatefulWidget {
  const IntercalateIndex({Key? key}) : super(key: key);

  @override
  State<IntercalateIndex> createState() => _IndexState();
}

class _IndexState extends State<IntercalateIndex> {

  //使用3G/4G/5G网络加载新闻
  late bool _IGLoding=false;
  //使用3G/4G/5G网络播放视频
  late bool _IGVoide=false;
  //动态页中在WiFi下自动播放视频
  late bool _IGInput=false;
  //使用在WiFi下自动加载数据
  late bool _IGInit=false;


  //自动缓存
  late bool _AoutInput=false;
  //WiFi网络下自动下载最新安装包
  late bool _WiFiInput=false;


  //允许外接蓝牙设备
  late bool _BlutInters=false;
  //连接车载蓝牙
  late bool _CarBlutInetr=false;

  //写入数据
  void setStorage(String Key,bool Value) async {
    SharedPreferences SP= await SharedPreferences.getInstance();
    SP.setBool(Key, Value);
  }

  //获取(初始)数据
  void getStorage() async {
    try{
      SharedPreferences SP= await SharedPreferences.getInstance();
      setState(() {
        _IGLoding = SP.getBool("IGLoding")!;
        _IGVoide= SP.getBool("IGVoide")!;
        _IGInput= SP.getBool("IGInput")!;
        _IGInit= SP.getBool("IGInit")!;

        _AoutInput=SP.getBool("AoutInput")!;
        _WiFiInput=SP.getBool("WiFiInput")!;

        _BlutInters=SP.getBool("BlutInters")!;
        _CarBlutInetr=SP.getBool("CarBlutInetr")!;
      });
    }catch(e){
      setStorage("IGLoding",false);
      setStorage("IGVoide",false);
      setStorage("IGInput",false);
      setStorage("IGInit",false);

      setStorage("AoutInput",false);
      setStorage("WiFiInput",false);

      setStorage("BlutInters",false);
      setStorage("CarBlutInetr",false);


    }
  }



  @override
  void initState() {
    // TODO: implement initState
    getStorage();
    super.initState();
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    setStorage("IGLoding",_IGLoding);
    setStorage("IGVoide",_IGVoide);
    setStorage("IGInput",_IGInput);
    setStorage("IGInit",_IGInit);

    setStorage("AoutInput",_AoutInput);
    setStorage("WiFiInput",_WiFiInput);

    setStorage("BlutInters",_BlutInters);
    setStorage("CarBlutInetr",_CarBlutInetr);
    super.deactivate();
  }



  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
        child: Container(
      padding: EdgeInsets.all(8),
      child: Column(
        children: [
          //网络设置
          Container(
            width: double.infinity,
            alignment: Alignment.center,
            decoration: BoxDecoration(
                color: Colors.black12,
                borderRadius: BorderRadius.all(Radius.circular(10))),
            child: Column(
              children: [
                Container(
                  child: Text(
                    "网络设置",
                    style: TextStyle(fontSize: 18),
                  ),
                  height: 36,
                  width: double.infinity,
                  margin: EdgeInsets.fromLTRB(10, 6, 0, 6),
                  alignment: Alignment.centerLeft,
                ),
                Divider(
                  height: 0,
                ),
                //使用3G/4G/5G网络加载新闻
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("使用3G/4G/5G网络加载新闻",style: TextStyle(fontSize: 18)),
                        CupertinoSwitch(
                            activeColor: Colors.blue,
                            value: _IGLoding,
                            onChanged: (value) {
                              setState(() {
                                _IGLoding = value;
                              });
                            })
                      ],
                    )),
                Divider(
                  height: 0,
                ),
                //使用3G/4G/5G网络播放视频
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("使用3G/4G/5G网络播放视频",style: TextStyle(fontSize: 18)),
                        CupertinoSwitch(
                            activeColor: Colors.blue,
                            value: _IGVoide,
                            onChanged: (value) {
                              setState(() {
                                _IGVoide = value;
                              });
                            })
                      ],
                    )),
                Divider(
                  height: 0,
                ),
                //动态页中在WiFi下自动播放视频
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("动态页中在WiFi下自动播放视频",style: TextStyle(fontSize: 18)),
                        CupertinoSwitch(
                            activeColor: Colors.blue,
                            value: _IGInput,
                            onChanged: (value) {
                              setState(() {
                                _IGInput = value;
                              });
                            })
                      ],
                    )),
                Divider(
                  height: 0,
                ),
                //使用在WiFi下自动加载数据
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("使用在WiFi下自动加载数据",style: TextStyle(fontSize: 18)),
                        CupertinoSwitch(
                            activeColor: Colors.blue,
                            value: _IGInit,
                            onChanged: (value) {
                              setState(() {
                                _IGInit = value;
                              });
                            })
                      ],
                    )),
              ],
            ),
          ),
          //工具
          Container(
            margin: EdgeInsets.fromLTRB(0, 8, 0, 0),
            width: double.infinity,
            alignment: Alignment.center,
            decoration: BoxDecoration(
                color: Colors.black12,
                borderRadius: BorderRadius.all(Radius.circular(10))),
            child: Column(
              children: [
                Container(
                  child: Text(
                    "工具",
                    style: TextStyle(fontSize: 18),
                  ),
                  height: 36,
                  width: double.infinity,
                  margin: EdgeInsets.fromLTRB(10, 6, 0, 6),
                  alignment: Alignment.centerLeft,
                ),
                Divider(
                  height: 0,
                ),
                //自动缓存
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("自动缓存",style: TextStyle(fontSize: 18)),
                        CupertinoSwitch(
                            activeColor: Colors.blue,
                            value: _AoutInput,
                            onChanged: (value) {
                              setState(() {
                                _AoutInput = value;
                              });
                            })
                      ],
                    )),
                Divider(
                  height: 0,
                ),
                //WiFi网络下自动下载最新安装包
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("WiFi网络下自动下载最新安装包",style: TextStyle(fontSize: 18)),
                        CupertinoSwitch(
                            activeColor: Colors.blue,
                            value: _WiFiInput,
                            onChanged: (value) {
                              setState(() {
                                _WiFiInput = value;
                              });
                            })
                      ],
                    )),
                Divider(
                  height: 0,
                ),
                //使用存储空间
                GestureDetector(
                  onTap: (){

                  },
                  child: Container(
                    alignment: Alignment.centerLeft,
                    width: double.infinity,
                    height: 36,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Text("使用存储空间",style: TextStyle(fontSize: 18)),
                  ),
                ),
              ],
            ),
          ),
          //蓝牙硬件
          Container(
            margin: EdgeInsets.fromLTRB(0, 8, 0, 0),
            width: double.infinity,
            alignment: Alignment.center,
            decoration: BoxDecoration(
                color: Colors.black12,
                borderRadius: BorderRadius.all(Radius.circular(10))),
            child: Column(
              children: [
                Container(
                  child: Text(
                    "蓝牙硬件",
                    style: TextStyle(fontSize: 18),
                  ),
                  height: 36,
                  width: double.infinity,
                  margin: EdgeInsets.fromLTRB(10, 6, 0, 6),
                  alignment: Alignment.centerLeft,
                ),
                Divider(
                  height: 0,
                ),
                //允许外接蓝牙设备
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("允许外接蓝牙设备",style: TextStyle(fontSize: 18)),
                        CupertinoSwitch(
                            activeColor: Colors.blue,
                            value: _BlutInters,
                            onChanged: (value) {
                              setState(() {
                                _BlutInters = value;
                              });
                            })
                      ],
                    )),
                Divider(
                  height: 0,
                ),
                //连接车载蓝牙
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("连接车载蓝牙",style: TextStyle(fontSize: 18)),
                        CupertinoSwitch(
                            activeColor: Colors.blue,
                            value: _CarBlutInetr,
                            onChanged: (value) {
                              setState(() {
                                _CarBlutInetr = value;
                              });
                            })
                      ],
                    )),
                Divider(
                  height: 0,
                ),
                //智能设备连接
                GestureDetector(
                  onTap: (){

                  },
                  child: Container(
                    alignment: Alignment.centerLeft,
                    width: double.infinity,
                    height: 36,
                    margin: EdgeInsets.fromLTRB(10, 6, 10, 6),
                    child: Text("智能设备连接",style: TextStyle(fontSize: 18)),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    ));
  }
}
